<template>
  <el-table
    :data="tableData"
    style="width: 100%">
    <el-table-column v-for="(item) in Object.keys(tableHeadData)" :key="item.bishe_id"
      :label="tableHeadData[item]"
      width="180">
      <template slot-scope="scope">
        <i class="el-icon-time"></i>
        <span style="margin-left: 10px">{{  scope.row[item]  }}</span>
      </template>
    </el-table-column>
    <!-- 选择的学生 -->
    <el-table-column
      label="选择的学生"
      width="180">
      <template slot-scope="scope">
        <el-popover trigger="hover" placement="top">
            <div v-for="(stu,index) in scope.row.student" :key="index">
                <p>编号: {{ stu.studentId }}</p>
                <p>姓名: {{ stu.username }}</p>
                <p>绩点: {{ stu.score }}</p>
                
            </div>
            <div slot="reference" class="name-wrapper" v-for="(stu,index) in scope.row.student" :key="index">
                    <el-tag slot="reference" size="medium">{{ stu.username }}</el-tag>
              </div>
        </el-popover>
      </template>
    </el-table-column>
     <el-table-column label="最终选择">
        <template slot-scope="scope">
          <el-select v-model="selected[scope.row.bisheId]" placeholder="请选择" >
              <el-option
                v-for="item in initOptions(scope.row.student)"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
          </el-select>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          class="my"
          size="mini"
          
          @click="finalChoose(scope.row)" :disabled="isButtonDisabled(scope.row)">选择</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import axios from 'axios'

export default {
    data(){
        return {
            //表头数据
            tableHeadData:{
                    bisheId: '毕设编号',
                    bisheName: '毕设标题',
                    univercity: '所属学院',
                    major: '所属专业',
                    createtime: '课设上传时间',
                    islock: '最终学生编号/已双向选择'
            },
            
            tableData:[],
            selected: {},
            
        }
    },
    methods:{
        initOptions(student){
          let n
          let options = []
          student.forEach(stu=>{
                  n = {}
                  n['value'] = stu.username
                  n['lable'] = stu.studentId
                  options.push(n)
              })

          
          return options
        },
        finalChoose(row){
          let islock = row.student.filter(e => e.username === this.selected[row.bisheId])[0].studentId
          
          

          this.$confirm('是否锁定最终学生？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(action => {
            if(action === 'confirm'){
              axios.post(`/prod-api/bishe/upDataIsLock`,{bisheId:row.bisheId,islock:islock})
                .then(response=>{
                  if (response.data.code === 0) {
                    this.$message({
                      type: 'success',
                      message: '选择成功'
                    })
                  }
                }).catch(error =>{
                   this.$router.push({path:'/error',params:{error:error}})
                })

            }
            // 接口传递信息是否选择成功，根据毕设是否被锁定来判断

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
        },
        logStudentInfo(student) {
          console.log(student);
        },
        isButtonDisabled(row){
            if(row.student.length === 0)
              return true
        }
    },
    created(){

        let teacher_id =  JSON.parse(localStorage.getItem('data')).tnub
        // 获取一对多毕设和学生关系数据
        
        console.log(teacher_id);
        axios.post(`/prod-api/xuanze/getStudentXuanze`,{teacherId:teacher_id})
                 .then(response=>{
                  
                     
                         this.tableData = response.data
                         
                         
                     
                    
                })

        
    }
}
</script>

<style>

</style>